//
//  HLJSoundCell.m
//  B段项目
//
//  Created by 111111 on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJSoundCell.h"

@implementation HLJSoundCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)createSubView{
    
    
    //类型
    self.type = [[UILabel alloc]init];
    [self.contentView addSubview:self.type];

    [self.type mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(120, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.left.and.top.mas_equalTo(20);
        
        
    }];

   
    self.type.font = [UIFont systemFontOfSize:WIDTH/25];
    self.type.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
   
    
    
    
    
    //图片
    self.soundImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.soundImageView];

    
    [self.soundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height. mas_equalTo(130);
        // 添加左、上边距约束（左、上约束都是20）
        make.left.mas_equalTo(70);
        make.right.mas_equalTo(- 70);
        make.top.mas_equalTo(60);
        
        
    }];

    
   
    
    
    
    //标题
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.numberOfLines = 0;
     [self.contentView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height. mas_equalTo(60);
        // 添加左、上边距约束（左、上约束都是20）
        make.left.mas_equalTo(80);
        make.right.mas_equalTo(- 70);
        make.top.mas_equalTo(230);
        
        
    }];
    self.titleLabel.font = [UIFont systemFontOfSize:20];

    
    //按钮背景
    self.playImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/6, WIDTH/6)];
    self.playImageView.backgroundColor = [UIColor colorWithRed:0.45 green:0.47 blue:0.46 alpha:1];
    self.playImageView.center = CGPointMake(WIDTH / 2, 60 + 130);
    self.playImageView.layer.masksToBounds = YES;
    self.playImageView.layer.cornerRadius = WIDTH/6/2;
    self.playImageView.alpha = 0.7;
    //设置边框宽度
    self.playImageView.layer.borderWidth = 3;
    //设置边框颜色
    self.playImageView.layer.borderColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1].CGColor;
    [self.contentView addSubview:self.playImageView];
  
    
    
    //钮图片
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/12, WIDTH/12)];
    imgView.image = [UIImage imageNamed:@"iconfont-play-2.png"];
    imgView.layer.masksToBounds = NO;
    imgView.layer.cornerRadius = WIDTH/24;
    imgView.center = CGPointMake(self.playImageView.frame.size.width/2 , self.playImageView.frame.size.width/2);
    [self.playImageView addSubview:imgView];
    
    
    //发布者
    self.author = [[UILabel alloc]init];
    [self.contentView addSubview:self.author];
    
    [self.author mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height. mas_equalTo(30);
        // 添加左、上边距约束（左、上约束都是20）
        make.left.mas_equalTo(135);
        make.right.mas_equalTo(- 135);
        make.top.mas_equalTo(300);
        
        
    }];

    self.author.font = [UIFont systemFontOfSize:WIDTH/25];
   // self.author.center = CGPointMake(WIDTH/2, 300);
    self.author.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    
    //心形图
    self.heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview: self.heartButton];
    
    [self.heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(30, 30));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-20);
        
        make.right.mas_equalTo(- 75);
        
        
        
    }];
    
    [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
    
     [self.heartButton addTarget:self action:@selector(heartButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //赞数
    self.zanLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.zanLabel];
    
    [self.zanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(35, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-25);
        
        make.right.mas_equalTo(- 25);
        
        
        
    }];
    
    self.zanLabel.font = [UIFont systemFontOfSize:12];
    self.zanLabel.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    
    
}




#pragma mark - 重写set方法
-(void)setSoundModel:(HLJHomeModel *)soundModel{
    _soundModel = soundModel;
    
    self.type.text = [NSString stringWithFormat:@"%@ · %@",self.soundModel.name,self.soundModel.enname];
    
    [self.soundImageView sd_setImageWithURL:soundModel.coverimg placeholderImage:PLACEHOLDER_IMAGE];
    self.titleLabel.text = soundModel.title;
    self.author.text = [NSString stringWithFormat:@"by:%@",[soundModel.userinfo objectForKey:@"uname"]];
    self.zanLabel.text = [NSString stringWithFormat:@"%@",soundModel.like];
    self.titleLabel.center = CGPointMake(WIDTH/2 , self.soundImageView.origin.y + self.soundImageView.height + self.type.origin.y/2*3);
    
    //判断收藏状态
    NSMutableArray *array = [[ZZCCollectionData collectDataBase]selectCollectList];
    NSMutableArray *array2 = [NSMutableArray array];
    for (HLJHomeModel *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.soundModel._id]) {
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    
}

- (void)heartButton:(UIButton *)btn
{
    NSMutableArray *array = [[ZZCCollectionData collectDataBase] selectCollectList];
    NSMutableArray *amptyArray = [NSMutableArray array];
    
    for (HLJHomeModel *model in array) {
        [amptyArray addObject:model._id];
    }
    if ([amptyArray containsObject:self.soundModel._id]) {
        [[ZZCCollectionData collectDataBase] deleteMyCollectData:self.soundModel.title];
        
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        keyAnimation.values = @[@(0.1),@(1.0),@(1.5)];
        keyAnimation.duration = 0.2;
        [btn.layer addAnimation:keyAnimation forKey:@"key"];
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] - 1];
    }else{
        HLJHomeModel *model = [[HLJHomeModel alloc]init];
        model._id = self.soundModel._id;
        model.title = self.soundModel.title;
        model.coverimg = self.soundModel.coverimg;
        model.name = self.soundModel.name;
        
        [[ZZCCollectionData collectDataBase]inserCollectList:model];
        
        //关键帧动画
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        //设置关键帧的值,values是数组类型
        //@(0.1)相当于NSNumber
        keyAnimtion.values = @[@(0.1),@(1.0),@(1.5)];//缩放的倍数
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        
        
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] + 1];
    }
    
    
    
}






@end
